package net.chrone.quickpayapi.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.chrone.quickpayapi.mapper.OrgFeeRateMapper;
import net.chrone.quickpayapi.model.Order;
import net.chrone.quickpayapi.model.OrgFeeRate;
import net.chrone.quickpayapi.model.OrgFeeRateKey;
import net.chrone.quickpayapi.service.OrgFeeRateService;
import net.chrone.quickpayapi.util.SdkUtil;
@Service
public class OrgFeeRateServiceImpl implements OrgFeeRateService {
	
	@Autowired
	private OrgFeeRateMapper orgFeeRateMapper;

	/**
	 * 按照机构代码+通道类型+交易类型计算手续费
	 */
	@Override
	public int caclFee(Order order) {
		OrgFeeRateKey key = new OrgFeeRateKey();
		key.setOrgId(order.getOrgId());
		key.setOrderTp(order.getOrderTp());
		key.setChnlTp(order.getTranTp());
		OrgFeeRate orgFeeRate = orgFeeRateMapper.selectByPrimaryKey(key);
		int fee = 0;
		if(0==orgFeeRate.getFeeType()){//百分比
			fee = SdkUtil.caclFee(order.getAmount(), orgFeeRate.getFeeRate());
		}else{//按笔
			Double df = 100*orgFeeRate.getFeeRate();
			fee = df.intValue();
		}
		return fee;
	}

}
